<template>
  <div class="playController">
    <div
      class="left"
      @click="show = !show"
    >
      <img
        :src="playlist[playCurrentIndex].al.picUrl"
        alt=""
      >
      <div class="content">
        <div class="title">{{playlist[playCurrentIndex].name}}</div>
        <div class="tips">横滑可以切换上下首歌</div>
      </div>
    </div>
    <div class="right">
      <svg
        class="icon"
        aria-hidden="true"
        @click="play"
        v-if="paused"
      >
        <use xlink:href="#icon-bofang2"></use>
      </svg>
      <svg
        class="icon"
        aria-hidden="true"
        @click="play"
        v-else
      >
        <use xlink:href="#icon-bofang"></use>
      </svg>
      <svg
        class="icon"
        aria-hidden="true"
      >
        <use xlink:href="#icon-bofangliebiao"></use>
      </svg>
    </div>
    <play-music
      v-show="show"
      :playDetail="playlist[playCurrentIndex]"
      @back="show=!show"
      :paused="paused"
      :play="play"
    />
    <audio
      ref="audio"
      :src="`https://music.163.com/song/media/outer/url?id=${playlist[playCurrentIndex].id}.mp3`"
      @timeupdate="UpdateTime"
    ></audio>
  </div>
</template>

<script>
  import {mapState,mapActions} from 'vuex'
  import playMusic from '@/components/playMusic.vue';
  export default {
    data() {
      return {
        paused:true,
        show:false
      }
    },
    components:{
      playMusic
    },
    computed:{
      ...mapState(['playlist','playCurrentIndex'])
    },
    updated() {
      this.reqLyric({
        id:this.playlist[this.playCurrentIndex].id
      });
    },
    watch: {
      playCurrentIndex(val,oldVal){
        this.$nextTick(()=>{
          this.$refs.audio.play();
          this.paused = false;
        })
      }
    },
    methods: {
      ...mapActions(['reqLyric']),
      play() {
        this.paused = !this.paused;
        if(this.$refs.audio.paused){
          this.$refs.audio.play();
          // this.UpdateTime();
        }else{
          this.$refs.audio.pause();
          /* if(this.$store.state.intervalId){
            clearInterval(this.$store.state.intervalId);
          } */
        }
      },
      UpdateTime () {
        // this.$store.state.intervalId = setInterval(() => {
          this.$store.commit('setCurrentTime',this.$refs.audio.currentTime);
        // }, 500);
      }
    },
  }
</script>

<style lang="less" scoped>
.playController {
  width: 7.5rem;
  height: 1.2rem;
  position: fixed;
  left: 0;
  bottom: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  background: #fff;
  .left {
    display: flex;
    padding: 0 0.2rem;
    img {
      width: 0.8rem;
      height: 0.8rem;
      border-radius: 0.4rem;
    }
    .content {
      padding: 0 0.2rem;
      .title {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 4rem;
      }
      .tips {
        font-size: 0.2rem;
        color: #999;
      }
    }
  }
  .right {
    .icon {
      width: 0.4rem;
      height: 0.4rem;
      margin: 0 0.2rem;
    }
  }
}
</style>